library(datasets)
data(iris)
#load packages
pkgs <- c("rstatix","emmeans","psych","tidyr","tidyverse","psych","dplyr","ggplot2","lm.beta","car","Hmisc","skimr","janitor")
lapply(pkgs, library, character.only = TRUE)
setwd("/Users/nataliesouza/Documents/DataScience/website")

Intro

Code Chunks

summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 
  • Most common
  • Use ` (backquote) not ’ (apostophe)
#Sum of 2 + 2
2+2
## [1] 4
#Average Sepal Length
mean(iris$Sepal.Length)
## [1] 5.843333
  • When exporting you will see both of these outputs, but will only see the last one within rmd.
  • You can also click the x on the upper right corner of the output to remove the output.
#Levels of Species
unique(iris$Species)
## [1] setosa     versicolor virginica 
## Levels: setosa versicolor virginica

Inline Code

Two plus two equals 4.
The average Sepal Length is 5.8433333.
The three levels of Species are setosa, versicolor, virginica.

Basics

Text

Plain text
End a line with two spaces to start a new paragraph.
italics and italics
bold and bold
superscript2
strikethrough
link

Headers (level 2)

Make sure to leave a space between the # and your text  
# Header 1
## Header 2

Header 3

Header 4

Header 5
Header 6

Symbols

endash: –
emdash: —
ellipsis: …
inline equation: \(A = \pi*r^{2}\)

horizontal rule (or slide break):


Formatting

block quote

  • unordered list
  • item 2
    • sub-item 1 (one tab)
    • sub-item 2 (one tab)
  1. ordered list
  2. item 2
    • sub-item 1 (two tabs)
    • sub-item 2 (two tabs)
First Header Second Header
Table Cell 1 Cell 2
Cell 3 Cell 4

Date

#Add this to date so that today's date is printed whenever doc is knitted
title: "Rmarkdown_Intro"
author: "Natalie Souza"
date: "`r format(Sys.Date(),'%e de %B, %Y')`"

Most of this information was taken from this useful cheatsheet

Code Specifications

Display Code

##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 

Display Warnings

warning=TRUE: Whether to display warnings

With Warning

data <- data.frame(x = 1:5,         # Create example data
                   y = 1:5)
ggp <- ggplot(data, aes(x, y)) +    # Create ggplot2 with default axis limits
  geom_point()

ggp +                               # Modify axis limits
  scale_x_continuous(limits = c(2, 5))
## Warning: Removed 1 rows containing missing values (geom_point).

Without Warning

data <- data.frame(x = 1:5,         # Create example data
                   y = 1:5)
ggp <- ggplot(data, aes(x, y)) +    # Create ggplot2 with default axis limits
  geom_point()

ggp +                               # Modify axis limits
  scale_x_continuous(limits = c(2, 5))

Display Errors

error=FALSE: Whether to display error messages
Default is error=TRUE

Display Output

Default results=FALSE

Output Included

summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 

Output Not Included

summary(iris)

Display Messages

Default message=TRUE

Messages Not Included

#load packages
pkgs <- c("rstatix","emmeans","psych","tidyr","tidyverse","psych","dplyr","ggplot2","lm.beta","car","Hmisc","skimr","janitor")
lapply(pkgs, library, character.only = TRUE)

Other Specifications

message=TRUE: Whether to display messages
eval=TRUE: Whether to evaluate the code and include its results
tidy=FALSE: Whether to reformat code in a tidy way when displaying it
fig.width=7: Width in inches for plots created in chunk
fig.height=7: Height in inches for plots created in chunk

Global Specifications

Bold Code

### <b>
x <- 10
y <- x * 2
### </b>

Table of Contents

Basic TOC

title: "Rmarkdown_Intro"
author: "Natalie Souza"
date: "`r format(Sys.Date(),'%e de %B, %Y')`"
output: 
  html_document
  toc: true
  toc_depth: 2 #This includes up to two headers

Floating TOC

title: "Rmarkdown_Intro"
author: "Natalie Souza"
date: "`r Sys.Date()`"
output: 
  html_document
  toc: true
  toc_float: true
  collapsed: true #If TOC appears with only top-level headers
  smooth_scroll: true #whether page scrolls are animated with mouse clicks
  toc_depth: 3 

Tabs

  • You can make it so that all subheadings are tabbed by using the {.tabset} command after your heading title.

Study 1

e.g. = study 1 graph

Study 2

e.g. = study 2 graph

Appearance

Themes

Built-In Themes

  • See here for a gallery of the different themes
#Specify this in the beginning of the doc (we are using the yeti theme)
theme: Specifes theme to use from the Bootswatch theme library

Specify: default, cerulean, journal, flatly, darkly, readable, spacelab, united, cosmo, lumen, paper, sandstone, simplex, and yeti (use null for no theme)

Downloadable Themes

  • Once you download/install this package you can use it when you create a new rmarkdown.
#There are a couple downloadable themes, and this is one
#This won't run automatically if you knit this scrit so you will need to run each line to install and load the package.
install.packages('rmdformats')
library(rmdformats)

Highlight

#Specify this in the beginning of the doc
Highlight: Specifes syntax highlighting style

Specify: default, tango, pygments, kate, monochrome, espresso, zenburn, haddock, breezedark, and textmate (use null for no highlight)

Smart

#Specify this in the beginning of the doc
Smart changes: straight quotes to curly quotes, --- to em-dashes, -- to en-dashes, and ... to ellipses. 

Smart is enabled by default

Data Frame Printing

#df_print: specifies how data is printed
#df_print = paged, creates a pageable table

output:
  html_document:
    df_print: paged

Figures

Embed Images

image: Moraine Lake in Canada
#Added {width=50%} to make it half the page, but don’t have to use this

Creating Images

#Specify this in the beginning of the doc
fig_width: 7
fig_height: 7
fig_caption: true #Adds captions to figures
dev: png #specifes type of imgaes figures will be

Advanced Inline Code

#run anova
model <- aov(Sepal.Length ~ Species, data = iris)
summary(model)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Species       2  63.21  31.606   119.3 <2e-16 ***
## Residuals   147  38.96   0.265                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#create clean tibble from output using broom package
library(broom)
modelmatrix <- broom::tidy(model)

#round to 3 decimals for clean output
modelmatrix$statistic <- sprintf(modelmatrix$statistic, fmt = '%#.3f') 
modelmatrix$p.value <- sprintf(modelmatrix$p.value, fmt = '%#.3f')

modelmatrix

There is a significant main effect of Species on Sepal Length, F(2,147) = 119.265, p = 0.000).